import axios from '@/utils/comm-request'
import urls from '@/api'
import { getToken } from '@/utils'

const state = {
  totalCount: 0,
  pageSize: 10,
  totalPage: 1,
  currPage: 1,
  list: [],
  selectedList: {},
  tempSelectd: [],
  selectLimit: 5,
  // 1视频、2图片、3文档、4音频、5动画、6应用
  cid: 0,
  propName: {}
}

const mutations = {
  /**
   * 更新列表数据
   * @param {*} data
   */
  updateData (state, data) {
    state.totalCount = data.totalCount
    state.pageSize = data.pageSize
    state.totalPage = data.totalPage
    state.currPage = data.currPage
    state.list = [...data.list]
    // 更新选中
    for (let i in state.tempSelectd) {
      const d = state.list.find(v => (v.filePath === state.tempSelectd[i].filePath))
      if (d) {
        mutations.toggleSelected(state, d)
        delete (state.tempSelectd[i])
      }
    }
  },
  updatePageSize (state, numb) {
    state.currPage = numb
  },
  /**
   * 设置参数
   * @param {*} data
   */
  setMaterialInfo (state, data) {
    if (data.cid) {
      state.cid = data.cid
    }
    if (data.limit) {
      state.selectLimit = data.limit
    }
    if (data.propName) {
      state.propName = { ...data.propName }
    }
    if (data.selected) {
      state.tempSelectd = [...data.selected]
    }
  },
  /**
   * 选中
   * @param {*} data
   */
  toggleSelected (state, data) {
    if(!data.animateInfo){
      data.animateInfo = {
        imgAnimates: '',
        imgSwitchTime: 2
      }
    }
    if (!data || !data.id) {
      return
    }
    if (state.selectLimit === 1) {
      state.selectedList = {}
      state.selectedList[data.id] = { ...data, tmp: new Date().getTime() }
      return
    }
    let { selectedList } = state
    if (selectedList[data.id]) {
      delete (selectedList[data.id])
    } else {
      selectedList[data.id] = { ...data, tmp: new Date().getTime() }
    }
    state.selectedList = { ...selectedList }
  },
  /**
   * 清空
   * @param {*} data
   */
  clearMaterialSelected (state, data) {
    state.selectedList = {}
  },
  clearMaterialData (state, data) {
    state.cid = 0
    state.totalCount = 0
    state.pageSize = 10
    state.totalPage = 1
    state.currPage = 1
    state.list = []
    state.selectedList = {}
    state.propName = ''
    state.tempSelectd = []
  }
}

const actions = {
  getMutations ({ state, commit }, params = {}) {
    const token = getToken()
    const { pageSize, currPage, cid } = state
    params = { ...params, cid, token, limit: pageSize, page: currPage }
    return axios.get(urls.getMaterialList, {
      params
    }).then(res => {
      const { page } = res
      if (!page) {
        console.error('get-mutations error', res)
        return
      }
      commit('updateData', page)
    })
  }
}

const getters = {
  uploadTypeName (state) {
    let { cid } = state
    cid = cid * 1
    return cid === 1 ? '视频'
      : cid === 2 ? '图片'
        : cid === 3 ? '文档'
          : cid === 4 ? '音频'
            : cid === 5 ? '动画'
              : cid === 6 ? '应用' : ''
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions,
  getters
}
